package com.xuecheng.content.controller;

import com.xuecheng.api.content.CoursePubApi;
import com.xuecheng.content.common.util.SecurityUtil;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程发布 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class CoursePubController implements CoursePubApi {

    @Autowired
    private CoursePubService  coursePubService;


    /*
     * SpringMVC 中controller 多结果视图器并存
     *      CourseBaseController ：
     *           JackSonResolver （默认）--controller使用的是RestController
     *           FreemarkerViewResolver
     *               1.添加依赖：spring-boot-starter-Freemarker
     *               2.添加配置：spring.freemarker.enabled = true
     *               3.方法的返回：controller的方法返回为ModelAndView对象
     * */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId, @PathVariable Long companyId) {

        Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);

        // 1.使用视图和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article",dataMap);

        return modelAndView;
    }

    /*
    课程发布
     */

    @PostMapping("course_pub/publish/{courseId}")
    public void publish(@PathVariable Long courseId) {
        //1.得到公司的id
        Long companyId = SecurityUtil.getCompanyId();
        coursePubService.publish(courseId,companyId);
    }









}
